﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;

namespace RDPSystem
{
    public partial class ServerObject : Component
    {
        public ServerObject()
        {
            InitializeComponent();
        }

        public ServerObject(IContainer container)
        {
            container.Add(this);

            InitializeComponent();            
        }

        static RDPRemoteObject.BroadCast oBroadCast = null;
        static string BroadCastUri = System.Configuration.ConfigurationManager.AppSettings["BroadCastUri"];

        static RDPRemoteObject.UserLogin oUserLogin = null;
        static string UserLoginUri = System.Configuration.ConfigurationManager.AppSettings["UserLoginUri"];

        /// <summary>
        /// 必须在客户端启动前初始化远程对象
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public object[] ServerObjectInit(object[] objs)
        {
            object[] rtn = new object[] { 0, null };
            if (oBroadCast == null)
            {
                oBroadCast = new RDPRemoteObject.BroadCast();
                oUserLogin = new RDPRemoteObject.UserLogin();
                try
                {
                    ObjRef objRefBroadCast = RemotingServices.Marshal(oBroadCast, BroadCastUri);                  
                    ObjRef objRefUserLogin = RemotingServices.Marshal(oUserLogin, UserLoginUri);
                }
                catch (Exception ex)
                {
                    rtn[0] = 1;
                    rtn[1] = ex;
                    oBroadCast = null;
                }
            }
            return rtn;
        }

        /// <summary>
        /// 向客户端广播消息
        /// </summary>
        /// <param name="msg"></param>
        public object[] BroadCastingInfo(object[] objs)
        {
            object[] rtn = new object[] { 0,null};
            object[] clients = objs[0] as object[];
            string msg = objs[1].ToString();
            try
            {
                object[] resp = oBroadCast.BroadCastingInfo(clients, msg);
                if (resp != null && Convert.ToInt32(resp[0])!=0)
                {
                    rtn = resp;
                }
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }

        /// <summary>
        /// 测试客户端是否在线
        /// </summary>
        /// <param name="msg"></param>
        public object[] OnLineTest(object[] objs)
        {
            object[] rtn = new object[] { 0, null };
            RDPCommon.Client.ClinetInfo msg = objs[0] as RDPCommon.Client.ClinetInfo;
            try
            {
                object[] resp = oUserLogin.OnLineTest(msg);
                if (resp != null && Convert.ToInt32(resp[0]) != 0)
                {
                    rtn = resp;
                }
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }

        /// <summary>
        /// 返回服务端活动用户
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public object[] GetServerActiveUsers(object[] objs)
        {
            object[] rtn = new object[] { 0, "", null };
            try
            {
                Dictionary<string, RDPCommon.Client.ClinetInfo>
                    dic = RDPRemoteObject.UserLogin.GetActiveUsers();
                rtn[1] = dic.Values.ToArray(); 
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }

        /// <summary>
        /// 返回服务端帐套
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public object[] GetServerDB(object[] objs)
        {
            object[] rtn = new object[] { 0, null };
            try
            {
                rtn[1] = RDPRemoteObject.DB.GetDB();
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
    }
}
